package com.tanhua.app.service;

import com.tanhua.api.mongo.PublishApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.mongo.Publish;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

// 服务圈子功能，向mq发送消息
@Service
public class QuanziMQService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference
    private PublishApi publishApi;

    // 发动态
    public void publishMsg(String publishId) {
        sendMsg(publishId, 1);
    }
    // 浏览动态
    public void queryPublishMsg(String publishId) {
        sendMsg(publishId, 2);
    }
    // 点赞
    public void likePublishMsg(String publishId) {
        sendMsg(publishId, 3);
    }
    // 喜欢
    public void lovePublishMsg(String publishId) {
        sendMsg(publishId, 4);
    }
    // 评论
    public void commentPublishMsg(String publishId) {
        sendMsg(publishId, 5);
    }
    // 取消点赞
    public void disLikePublishMsg(String publishId) {
        sendMsg(publishId, 6);
    }
    // 取消喜欢
    public void disLovePublishMsg(String publishId) {
        sendMsg(publishId, 7);
    }
    /*
        发送圈子操作行为
            type
                1-发动态，2-浏览动态，3-点赞，4-喜欢，5-评论，6-取消点赞，7-取消喜欢
     */
    private void sendMsg(String publishId, Integer type) {
        // 根据publishId 查询pid
        Publish publish = publishApi.findById(publishId);

        // 构建消息内容
        Map<String, Object> message = new HashMap<>();
        message.put("publishId", publish.getPid()); // 大数据需要的动态id
        message.put("type", type); // 用户操作类型
        message.put("userId", UserHolder.getUserId()); // 用户id

        rocketMQTemplate.convertAndSend("quanzi-recommend", message);
    }
}
